$(function () {
  let $navul = $('.navul');
  let $info = $('.info')
  let percent = 0;
  let yiling = 0
  let shengyu = 126


  function e(res) {
    let ulStr = ''
    res.result.forEach(element => {
      ulStr += `    <li id=${element.titleId}>
    <a href="">${element.title}</a>
  </li>`
    });
    $navul.append(ulStr)
    $('.bar_in').css({ width: '0px' })


    let stop1 = setInterval(() => {

      let str = `<span>${~~percent}%</span>`
      let yilingStr = `<span>已领：${~~yiling + 1}/剩余：${~~shengyu + 1}</span>`

      width = $('.bar_in').width()
      if (percent <= 100) {
        percent += .47;
        shengyu -= .6;
        yiling += .6;
      }

      $('.bar_in').html(str)
      $('.yiling').html(yilingStr)

      if (width < 212) {
        var inWidth = (width + 1) + 'px'
        $('.bar_in').css({
          width: inWidth
        })
      }
      else {
        clearInterval(stop1)
      }
    }, 20);
  }

  function a(res) {
    let infoStr = ''
    let regExp = /^<[^<]+\s+src="([^''""]*)"/
    res.result.forEach(element => {
      let imgUrl = element.productImg.match(regExp)
      infoStr += `
    <li>
    <a href="">
      <div class="img">
        <img src="${imgUrl[1]}" alt="">
      </div>
      <div class="infoR">
        <div class="title">
          <h4>${element.productName}</h4>
          <div class="price">${element.productPrice}<del>1888.0</del>
          </div>
        </div>
        <div class="down">
          <div class="bar">
            <div class="bar_in" style="100%"></div>
          </div>
          <span class="yiling"></span>
          <div class="click">
            <div class="left">点此领取优惠券</div>
            <div class="right">下单链接</div>
          </div>
        </div>
      </div>
    </a>
  </li>`
    });
    $info.html(infoStr)
    $('.bar_in').css({ width: '0px' })
    percent = 0;
    shengyu = 126;
    yiling = 0;

    let stop2 = setInterval(() => {
      width = $('.bar_in').width()
      if (percent <= 100) {
        percent += .47;
        shengyu -= .6;
        yiling += .6;
      }
      let str = `<span>${~~percent}%</span>`
      let yilingStr = `<span>已领：${~~yiling + 1}/剩余：${~~shengyu + 1}</span>`
      $('.bar_in').html(str);
      $('.yiling').html(yilingStr);
      var inWidth = 0;
      if (width < 212) {
        inWidth = (width + 1) + 'px'
        $('.bar_in').css({
          width: inWidth
        })
      } else {
        clearInterval(stop2)
      }
    }, 20);
  }

  let init = {
    url: 'http://chst.vip:1234/api/getbaicaijiatitle',
    success: e
  }

  let inofInit = {
    url: 'http://chst.vip:1234/api/getbaicaijiaproduct?titleid=0',
    success: a
  }

  ajaxAPI(inofInit)

  ajaxAPI(init)

  $navul.on('click', 'li', function (e) {
    e.preventDefault()
    let titleid = $(this).attr('id');
    let ulItems = {
      url: 'http://chst.vip:1234/api/getbaicaijiaproduct?titleid=' + titleid,
      success: a
    }
    ajaxAPI(ulItems)
  })
})



